package com.wg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wg.entity.Subtask;
import com.wg.mapper.SubTaskMapper;
import com.wg.service.SubtaskService;
import com.wg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SubtaskServiceImpl extends ServiceImpl<SubTaskMapper, Subtask> implements SubtaskService {

    @Autowired
    UserService userService;

    @Override
    public List<Subtask> getByWgId(Long wgId) {
        LambdaQueryWrapper<Subtask> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Subtask::getWgId, wgId);
        List<Subtask> subtaskList = list(wrapper);
        subtaskList = subtaskList.stream().map(task ->
            task.setAssignName(userService.getById(task.getAssignId()).getName())
        ).collect(Collectors.toList());
        return subtaskList;
    }

    @Override
    public Page<Subtask> selectByAssignId(Long assignId, Integer pageSize, Integer pageNum) {
        LambdaQueryWrapper<Subtask> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Subtask::getAssignId, assignId);
        // 只需查找id, wgId
        wrapper.select(Subtask::getId, Subtask::getWgId);
        Page<Subtask> subtaskPage = new Page<>(pageNum, pageSize);
        page(subtaskPage, wrapper);
        return subtaskPage;
    }


}
